﻿using System.ComponentModel;
using System.Linq;
using System.Web.UI;
using Ext.Net;
using Ext.Net.Utilities;
using Container = Ext.Net.Container;

namespace KamLib.Controls
{

    #region Containers

    [ParseChildren(true, "Items")]
    public class LayoutContainer : Container
    {
        [DefaultValue(1)]
        public override int Flex
        {
            get { return ViewState.Try("Flex", 1); }
            set { base.Flex = value; }
        }

        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public override ItemsCollection<Ext.Net.Component> Items { get { return base.Items; } }
    }

    public class Absolute : LayoutContainer
    {
        [DefaultValue("Absolute")]
        public override string Layout { get { return "Absolute"; } set { } }
    }

    public class Accordion : LayoutContainer
    {
        [DefaultValue("Accordion")]
        public override string Layout { get { return "Accordion"; } set { } }

        public override LayoutConfigCollection LayoutConfig
        {
            get
            {
                var l = base.LayoutConfig;
                if (l.Count == 0)
                    l.Add(new AccordionLayoutConfig { OriginalHeader = true, Animate = true });
                return l;
            }
        }
    }

    public class Anchor : LayoutContainer
    {
        [DefaultValue("Anchor")]
        public override string Layout { get { return "Anchor"; } set { } }
    }

    public class Card : LayoutContainer
    {
        [DefaultValue("Card")]
        public override string Layout { get { return "Card"; } set { } }
    }

    public class Fit : LayoutContainer
    {
        [DefaultValue("Fit")]
        public override string Layout { get { return "Fit"; } set { } }
    }

    public class Form : LayoutContainer
    {
        [DefaultValue("Form")]
        public override string Layout { get { return "Form"; } set { } }
    }

    public class HBox : LayoutContainer
    {
        [Category("6. KamLib")]
        [DefaultValue(HBoxAlign.Top)]
        [NotifyParentProperty(true)]
        public HBoxAlign Align { get; set; }

        [DefaultValue("HBox")]
        public override string Layout { get { return "HBox"; } set { } }

        [Category("6. KamLib")]
        [DefaultValue(false)]
        [NotifyParentProperty(true)]
        public bool Stretch { get { return Align == HBoxAlign.Stretch; } set { Align = value ? HBoxAlign.Stretch : HBoxAlign.Top; } }

        public override LayoutConfigCollection LayoutConfig
        {
            get
            {
                var l = base.LayoutConfig;
                if (Stretch && l.Count == 0)
                    l.Add(new HBoxLayoutConfig { Align = Align });
                return l;
            }
        }
    }

    public class VBox : LayoutContainer
    {
        [Category("6. KamLib")]
        [DefaultValue(VBoxAlign.Left)]
        [NotifyParentProperty(true)]
        public VBoxAlign Align { get; set; }

        [DefaultValue("VBox")]
        public override string Layout { get { return "VBox"; } set { } }

        [Category("6. KamLib")]
        [DefaultValue(false)]
        [NotifyParentProperty(true)]
        public bool Stretch { get { return Align == VBoxAlign.Stretch; } set { Align = value ? VBoxAlign.Stretch : VBoxAlign.Left; } }

        public override LayoutConfigCollection LayoutConfig
        {
            get
            {
                var l = base.LayoutConfig;
                if (Stretch && l.Count == 0)
                    l.Add(new VBoxLayoutConfig { Align = Align });
                return l;
            }
        }
    }

    #endregion

    #region Panels

    [ParseChildren(true, "Items")]
    public class LayoutPanel : Panel
    {
        [DefaultValue(false)]
        public override bool Border { get { return false; } set { } }

        [DefaultValue(1)]
        public override int Flex
        {
            get { return ViewState.Try("Flex", 1); }
            set { base.Flex = value; }
        }

        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public override ItemsCollection<Ext.Net.Component> Items { get { return base.Items; } }
    }


    public class AccordionPanel : LayoutPanel
    {
        [DefaultValue("Accordion")]
        public override string Layout { get { return "Accordion"; } set { } }

        public override LayoutConfigCollection LayoutConfig
        {
            get
            {
                var l = base.LayoutConfig;
                if (l.Count == 0)
                    l.Add(new AccordionLayoutConfig { OriginalHeader = true, Animate = true });
                return l;
            }
        }
    }

    public class AnchorPanel : LayoutPanel
    {
        [DefaultValue("Anchor")]
        public override string Layout { get { return "Anchor"; } set { } }
    }

    public class FitPanel : LayoutPanel
    {
        [DefaultValue("Fit")]
        public override string Layout { get { return "Fit"; } set { } }
    }

    public class FormLayoutPanel : LayoutPanel
    {
        [DefaultValue("Form")]
        public override string Layout { get { return "Form"; } set { } }
    }

    public class HBoxPanel : LayoutPanel
    {
        [Category("6. KamLib")]
        [DefaultValue(HBoxAlign.Top)]
        [NotifyParentProperty(true)]
        public HBoxAlign Align { get; set; }

        [DefaultValue("HBox")]
        public override string Layout { get { return "HBox"; } set { } }

        [Category("6. KamLib")]
        [DefaultValue(false)]
        [NotifyParentProperty(true)]
        public bool Stretch { get { return Align == HBoxAlign.Stretch; } set { Align = value ? HBoxAlign.Stretch : HBoxAlign.Top; } }

        public override LayoutConfigCollection LayoutConfig
        {
            get
            {
                var l = base.LayoutConfig;
                if (Stretch && l.Count == 0)
                    l.Add(new HBoxLayoutConfig { Align = Align });
                return l;
            }
        }
    }

    public class VBoxPanel : LayoutPanel
    {
        [Category("6. KamLib")]
        [DefaultValue(VBoxAlign.Left)]
        [NotifyParentProperty(true)]
        public VBoxAlign Align { get; set; }

        [DefaultValue("VBox")]
        public override string Layout { get { return "VBox"; } set { } }

        [Category("6. KamLib")]
        [DefaultValue(false)]
        [NotifyParentProperty(true)]
        public bool Stretch { get { return Align == VBoxAlign.Stretch; } set { Align = value ? VBoxAlign.Stretch : VBoxAlign.Left; } }

        public override LayoutConfigCollection LayoutConfig
        {
            get
            {
                var l = base.LayoutConfig;
                if (Stretch && l.Count == 0)
                    l.Add(new VBoxLayoutConfig { Align = Align });
                return l;
            }
        }
    }

    #endregion

    #region FieldSets

    [ParseChildren(true, "Items")]
    public class LayoutFieldSet : Ext.Net.FieldSet
    {
        [DefaultValue(1)]
        public override int Flex
        {
            get { return ViewState.Try("Flex", 1); }
            set { base.Flex = value; }
        }

        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public override ItemsCollection<Ext.Net.Component> Items { get { return base.Items; } }

        public override string CheckboxName
        {
            get { return base.CheckboxName; }
            set
            {
                base.CheckboxName = value;
                if (value.IsNotEmpty())
                    CheckboxToggle = true;
            }
        }
    }

    public class FieldSet : LayoutFieldSet
    {
        [DefaultValue("Form")]
        public override string Layout { get { return "Form"; } set { } }
    }

    public class FieldSetAccordion : LayoutFieldSet
    {
        [DefaultValue("Accordion")]
        public override string Layout { get { return "Accordion"; } set { } }

        public override LayoutConfigCollection LayoutConfig
        {
            get
            {
                var l = base.LayoutConfig;
                if (l.Count == 0)
                    l.Add(new AccordionLayoutConfig { OriginalHeader = true, Animate = true });
                return l;
            }
        }
    }

    public class FieldSetAnchor : LayoutFieldSet
    {
        [DefaultValue("Anchor")]
        public override string Layout { get { return "Anchor"; } set { } }
    }

    public class FieldSetFit : LayoutFieldSet
    {
        [DefaultValue("Fit")]
        public override string Layout { get { return "Fit"; } set { } }
    }

    public class FieldSetForm : LayoutFieldSet
    {
        [DefaultValue("Form")]
        public override string Layout { get { return "Form"; } set { } }
    }

    public class FieldSetHBox : LayoutFieldSet
    {
        [Category("6. KamLib")]
        [DefaultValue(HBoxAlign.Top)]
        [NotifyParentProperty(true)]
        public HBoxAlign Align { get; set; }

        [DefaultValue("HBox")]
        public override string Layout { get { return "HBox"; } set { } }

        [Category("6. KamLib")]
        [DefaultValue(false)]
        [NotifyParentProperty(true)]
        public bool Stretch { get { return Align == HBoxAlign.Stretch; } set { Align = value ? HBoxAlign.Stretch : HBoxAlign.Top; } }

        public override LayoutConfigCollection LayoutConfig
        {
            get
            {
                var l = base.LayoutConfig;
                if (Stretch && l.Count == 0)
                    l.Add(new HBoxLayoutConfig { Align = Align });
                return l;
            }
        }
    }

    public class FieldSetVBox : LayoutFieldSet
    {
        [Category("6. KamLib")]
        [DefaultValue(VBoxAlign.Left)]
        [NotifyParentProperty(true)]
        public VBoxAlign Align { get; set; }

        [DefaultValue("VBox")]
        public override string Layout { get { return "VBox"; } set { } }

        [Category("6. KamLib")]
        [DefaultValue(false)]
        [NotifyParentProperty(true)]
        public bool Stretch { get { return Align == VBoxAlign.Stretch; } set { Align = value ? VBoxAlign.Stretch : VBoxAlign.Left; } }

        public override LayoutConfigCollection LayoutConfig
        {
            get
            {
                var l = base.LayoutConfig;
                if (Stretch && l.Count == 0)
                    l.Add(new VBoxLayoutConfig { Align = Align });
                return l;
            }
        }
    }

    #endregion


}
